﻿using Dispatch.DispatchData;
using Dispatch.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dispatch
{
    /// <summary>
    /// 数据初始化类，只需要执行一次InitData()即可
    /// </summary>
    internal class DataInit
    {

        public void InitData()
        {
            Task t1 = InitDeviceAsync();
            Task t2 = InitAgvAsync();
            Task t3 = InitAreaAsync();
            Task t4 = InitMarkerAndRouteAsync();

            Task.WaitAll(t1, t2, t3, t4);
        }




        #region 区域

        //从数据库加载
        List<WarningAreaModal> LoadAreaFromDB()
        {
            List<WarningAreaModal> list = new List<WarningAreaModal>();

            return list;
        }

        //从缓存中加载（redis，mongodb等）
        List<WaringArea> LoadAreaFromCache()
        {
            List<WaringArea> list = new List<WaringArea>();
            return list;
        }

        /// <summary>
        /// 初始化区域
        /// </summary>
        /// <returns></returns>
        Task InitAreaAsync()
        {
            List<WarningAreaModal> dblist = LoadAreaFromDB();
            List<WaringArea> cacheList = LoadAreaFromCache();

            List<WaringArea> list = new List<WaringArea>();
            dblist.ForEach(item =>
            {
                WaringArea obj = new WaringArea();
                obj.AreaID = item.AreaID;
                obj.PointList.AddRange(item.pointList);
                obj.MaxX = (int)item.pointList.Max(x => x.X);
                obj.MinX = (int)item.pointList.Min(x => x.X);
                obj.MaxY = (int)item.pointList.Max(x => x.Y);
                obj.MinY = (int)item.pointList.Min(x => x.Y);

                var cache = cacheList.Find(x => x.AreaID == item.AreaID);
                if (cache != null)
                {
                    //更新锁定数据
                    obj.Owner = cache.Owner;
                    obj.Locked = cache.Locked;
                    obj.StopList = cache.StopList;
                }
                list.Add(obj);
            });
            WarningAreaDataSet.FillData(list);
            return Task.CompletedTask;
        }


        #endregion


        #region 地标和路由

        /// <summary>
        /// 从数据库中加载
        /// </summary>
        /// <returns></returns>
        List<MarkerModel> LoadMarkerFromDB()
        {
            List<MarkerModel> list = new List<MarkerModel>();

            return list;
        }

        //从缓存中加载待命点数据（redis，mongodb等）
        List<StayMarker> LoadStayFromCache()
        {
            List<StayMarker> list = new List<StayMarker>();
            return list;
        }




        //从数据库加载
        List<RouteModel> LoadRouteFromDB()
        {
            List<RouteModel> list = new List<RouteModel>();
            return list;
        }


        /// <summary>
        /// 初始化地标、待命点和路由
        /// </summary>
        /// <returns></returns>
        Task InitMarkerAndRouteAsync()
        {
            //地标
            List<MarkerModel> mdbList = LoadMarkerFromDB();
            MarkerDataSet.FillData(mdbList);

            //待命
            List<StayMarker> scacheList = LoadStayFromCache();
            var slist = mdbList.Where(x => x.Type == (int)MarkerEnum.待命).ToList();
            if (slist.Count > 0)
            {
                List<StayMarker> list = new List<StayMarker>();
                //设计了待命点
                slist.ForEach(item =>
                {
                    StayMarker obj = new StayMarker();
                    obj.MarkerID = item.MarkerID;
                    obj.Process = item.Process;
                    var cache = scacheList.Find(x => x.MarkerID == item.MarkerID);
                    if (cache != null)
                    {
                        obj.AgvID = cache.AgvID;
                        obj.LockTime = cache.LockTime;
                    }
                    list.Add(obj);
                });
                MarkerDataSet.FillStayData(list);
            }


            //路由
            List<RouteModel> routeList = LoadRouteFromDB();
            RouteDataSet.FillData(routeList);
            return Task.CompletedTask;
        }



        #endregion


        #region 传输坞
        /// <summary>
        /// 从数据库中加载
        /// </summary>
        /// <returns></returns>
        List<DeviceModel> LoadDeviceFromDB()
        {
            List<DeviceModel> list = new List<DeviceModel>();

            //测试
            for(int i = 1; i < 31; i++)
            {
                DeviceModel obj=new DeviceModel();
                obj.DeviceID = 1000+i;
                obj.IP = "127.0.0.1";
                obj.Port = 8888;
                obj.SendPort = 7788;
                obj.Process = "抛光";
                obj.DockNum = i;
                obj.UpOut = true;
                obj.DownOut = false;
                list.Add(obj);
            }

            return list;
        }

        //从缓存中加载传输坞数量（redis，mongodb等）
        List<DeviceMountData> LoadMountFromCache()
        {
            List<DeviceMountData> list = new List<DeviceMountData>();
            return list;
        }


        //从缓存中加载机台和AGV之间的关系
        Dictionary<long, int> LoadAgvDeviceFromCache()
        {
            Dictionary<long, int> dict = new Dictionary<long, int>();
            return dict;
        }

        Task InitDeviceAsync()
        {
            List<DeviceModel> dbList = LoadDeviceFromDB();
            List<DeviceMountData> cacheList = LoadMountFromCache();

            DeviceMountSet.FillBaseData(dbList);

            if (cacheList.Count > 0)
            {
                DeviceMountSet.FillMountData(cacheList);
            }


            Dictionary<long, int> dict = LoadAgvDeviceFromCache();
            if (dict.Count > 0)
            {
                DeviceMountSet.FillAgv(dict);
            }
            return Task.CompletedTask;
        }


        #endregion



        #region AGV

        /// <summary>
        /// 从数据库中加载
        /// </summary>
        /// <returns></returns>
        List<AgvModel> LoadAgvFromDB()
        {
            List<AgvModel> list = new List<AgvModel>();

            return list;
        }

       

        Task InitAgvAsync()
        {
            List<AgvModel> dblist = LoadAgvFromDB();
            if (dblist.Count > 0)
            {
                List<AgvData> list = new List<AgvData>();
                foreach (var agv in dblist)
                {
                    AgvData obj = new AgvData();
                    obj.AgvID = agv.AgvID;
                    obj.IP = agv.IP;
                    obj.Port = agv.Port;

                    list.Add(obj);
                }
                AgvDataSet.FillData(list);
            }
 
            return Task.CompletedTask;
        }

        #endregion
    }
}
